package com.syeet.gulimall.cart.controller;

import com.syeet.gulimall.cart.service.CartService;
import com.syeet.gulimall.cart.vo.Cart;
import com.syeet.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author xhl
 * @date 2023/4/25
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping(value = "/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){
        return  cartService.getCurrentUserCartItems();
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public  String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改购物项数量
     * @return
     */
    @GetMapping(value = "/countItem")
    public String changeItem(@RequestParam("skuId") Long skuId,
                             @RequestParam("num") Integer num){
        cartService.changeItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 改变购物项选中状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping(value = "/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check) {
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 浏览器有一个cookie；user-key,标识用户信息，一个月过期
     * 如果第一次使用jd购物车，都会给一个user-key
     * 浏览器保存，以后每次访问都会带上该cookie
     * <p>
     * 第一次，如果没有临时用户，创建一个
     *
     * @return
     */
    @GetMapping(value = "/cart.html")
    public String page(Model model) throws Exception {
        //快速得到用户信息id,user-key
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * redirectAttributes.addAttribute("skuId", skuId);将数据拼接到url后面 http://cart.gulimall.com/addToCartSuccess.html?skuId=4
     * redirectAttributes.addFlashAttribute(),将数据放到session中，可以在页面取出，但只能取一次
     *
     * @return
     */
    @GetMapping(value = "/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws Exception {
        cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页，避免重复刷页面，重复添加相同数据
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping(value = "/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getCartItemBySkuId(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }
}
